package com.breakfast.fun;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.breakfast.fun.event.FinishEvent;
import com.breakfast.fun.event.RefreshEvent;
import com.breakfast.fun.ui.home.HomeFragment;
import com.breakfast.fun.ui.mall.MallFragment;
import com.breakfast.fun.ui.user.UserFragment;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.StatusBarUtil;
import com.fm.openinstall.OpenInstall;
import com.fm.openinstall.listener.AppWakeUpListener;
import com.fm.openinstall.model.AppData;
import com.fm.openinstall.model.Error;
import com.google.android.gms.analytics.Tracker;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.fl_main)
    FrameLayout flMain;
    @Bind(R.id.rg_main)
    RadioGroup rgMain;


    @Bind(R.id.rb_main_bottom_home)
    RadioButton rbMainBottomHome;
    @Bind(R.id.rb_main_bottom_fans)
    RadioButton rbMainBottomFans;
    @Bind(R.id.rb_main_bottom_user)
    RadioButton rbMainBottomUser;

    private HomeFragment homeFragment;
    private MallFragment mMallFragment;
    private UserFragment userFragment;

    private Tracker mTracker;

    private Long firstTime = 0L;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        StatusBarUtil.transparent(getWindow());

        EventBus.getDefault().register(this);

        mTracker = App.getApp().getDefaultTracker();

        rbMainBottomHome.setChecked(true);
        initView();

        onNewIntent(getIntent());
    }

    private void initView() {
        homeFragment = new HomeFragment();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fl_main, homeFragment)
                .commit();
        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb_main_bottom_home:
                        setTabSelection(0);
                        break;
                    case R.id.rb_main_bottom_fans:
                        setTabSelection(1);
                        break;
                    case R.id.rb_main_bottom_user:
                        setTabSelection(2);
                        break;
                    default:
                        setTabSelection(0);
                        break;
                }
            }
        });
    }


    public void setTabSelection(int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideAllFragments(transaction);

        switch (index) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.fl_main, homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                break;
            case 1:
                if (mMallFragment == null) {
                    mMallFragment = new MallFragment();
                    transaction.add(R.id.fl_main, mMallFragment);
                } else {
                    transaction.show(mMallFragment);
                }
                break;
            case 2:
                if (userFragment == null) {
                    userFragment = new UserFragment();
                    transaction.add(R.id.fl_main, userFragment);
                } else {
                    transaction.show(userFragment);
                }
                break;
        }

        transaction.commitAllowingStateLoss();
    }

    private void hideAllFragments(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (userFragment != null) {
            transaction.hide(userFragment);
        }
        if (mMallFragment != null) {
            transaction.hide(mMallFragment);
        }
    }

    @Subscribe
    public void onEvent(RefreshEvent event) {
        if (event.getActivityName().equals(this.getClass().getSimpleName())) {
            rbMainBottomHome.setChecked(true);
        }
    }

    @Subscribe
    public void onEvent(FinishEvent event) {
        if (event.getActivityName().equals("LoginActivity")) {
            setTabSelection(0);
            rbMainBottomHome.setChecked(true);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    public boolean dispatchKeyEvent(KeyEvent event) {

        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            Long t = System.currentTimeMillis();
            if (t - firstTime < 1000 * 2) {
                MainActivity.this.finish();
            } else {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            }
            firstTime = t;
            return true;

        }
        return super.dispatchKeyEvent(event);

    }


}
